/*
 * @Author: HanYaJun
 * @Date: 2023-01-11 10:33:11
 * @Email: hanyajun@wedobest.com.cn
 * @Description: 星消灭改变方块颜色
 */

/**
 * @description: 方块类型
 */
export enum imageTypeEumn {
    /**蓝色 */
    BLUE = 1,
    /**绿色 */
    GREEN = 2,
    /**粉色 */
    PINK = 3,
    /**红色 */
    RED = 4,
    /**黄色 */
    YELLOW = 5
}

import { GTR } from '../../../Public/GTR';
import xxmGameEvents from '../Global/xxmGameEvents';

let trace = function (...args) {
    GTR.logg.log("xxmChangeBlock【HYJ】", ...args);
}
let traceError = function (...args) {
    GTR.logg.error("xxmChangeBlock【HYJ】", ...args);
}

const { ccclass, property } = cc._decorator;
@ccclass
export class xxmChangeBlock extends cc.Component {
    /**图片类型 */
    private imageType: number = null;

    /**
     * @description: 当附加到一个激活的节点上或者其节点第一次激活时候调用
     */
    protected onLoad(): void {
        // 递归查找自身或所有子节点中指定类型的组件
        let buttons: cc.Button[] = this.node.getComponentsInChildren(cc.Button);
        buttons.forEach((button) => {
            button.node.on('click', this.btnClickCallBack, this);
        });
    }

    /**
     * @description: 方块数据初始化
     */
    public initData(data: { colorValue: number }): void {
        this.imageType = data.colorValue;
        this.createBlockImg(data.colorValue);
    }

    /**
     * @description: 随机创建方块图片
     * @param {number} index
     * @return {*}
     */
    private createBlockImg(imageIndex: number): void {
        // 图片地址
        let pictureStr: string = `res_xxm/pub/image/play/block/card_${imageIndex}/spriteFrame`;
        // 方块图片加载
        GTR.res.load(pictureStr, cc.SpriteFrame, (error: any, spriteFrame: cc.SpriteFrame) => {
            if (!error) {
                let blockImageSprite: cc.Sprite = this.node.getComponent(cc.Sprite);
                blockImageSprite.spriteFrame = spriteFrame;
            } else {
                traceError('图片加载错误error:', error);
            }
        });
    }

    /**
     * @description: 设置图片类型
     * @param {imageTypeEumn} num
     */
    public setImageType(num: imageTypeEumn) {
        this.imageType = num;
    }

    /**
     * @description: 获取图片类型
     */
    public getImageType(): imageTypeEumn {
        return this.imageType;
    }

    /**
     * @description: 点击回调
     */
    private btnClickCallBack(): void {
        GTR.event.dispatchEvent(xxmGameEvents.CHANGE_CLICK_COLOR, { imageType: this.imageType });
    }

    /**
     * @description: 当该组件被销毁时调用
     */
    protected onDestroy(): void {
    }
}
